<?php
/**
 * Name: IP白名单中间件.
 * User: 董坤鸿
 * Date: 2017/2/17
 * Time: 17:26
 */

namespace App\Http\Middleware;

use Closure;

class IpWhiteList
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $ip = request()->getClientIp();
        $ipWhite = explode(',', env('IP_WHITE'));
        if (!in_array($ip, $ipWhite)) {
            abort(401, '您的IP不允许访问');
        }
        return $next($request);
    }
}
